package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseKcgl;
import top.jnaw.jee.platform.model.base.BaseSteps;
import top.jnaw.jee.platform.model.base.BaseVersion;
import top.jnaw.jee.utils.Html2Test;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Kcgl extends BaseKcgl<Kcgl> {

  public static final Kcgl dao = new Kcgl().dao();

  public static int add(int bid, String title,
      String content, JSONArray steps) {
    ModelResult result = new ModelResult(Result.kModelSaveError);
    JSONArray accessory = new JSONArray();
    Kcgl kcgl = new Kcgl();
    if (bid > 0) {
      kcgl.setBid(bid);
    }
    kcgl.setTitle(title);
    kcgl.setContent(content);
    kcgl.setPureText(Html2Test.getTextFromHtml(content));
    boolean r = kcgl.save();
    int id = -1;
    if (r) {
      id = kcgl.getId();
    }
    return id;
  }

  public static int update(int id, int bid, String title,
      String content, JSONArray steps) {
    ModelResult result = new ModelResult(Result.kModelUpdateError);
    Kcgl kcgl = Kcgl.dao.findById(id);
    int iid = -1;
    if (null != kcgl) {
      kcgl.setBid(bid);
      kcgl.setTitle(title);
      kcgl.setContent(content);
      kcgl.setPureText(Html2Test.getTextFromHtml(content));
      boolean r = kcgl.update();
      if (r) {
        iid = kcgl.getId();
      }
    }
    return iid;
  }

  public static boolean delete(int id) {
    Kcgl kcgl = Kcgl.dao.findById(id);
    int bid = kcgl.getBid();
    boolean r = Badge.delete(bid);
    if (r) {
      r = Kcgl.dao.deleteById(id);
    }
    //Db.update("delete from kcgl where id=" + id);
    return r;
  }

  public static JSONObject list(int page, int size, String keyWord,
      String openid, String status) {

    String sec = Db.getSql("kcgl-list-sec");
    String table = Db.getSql("kcgl-list-table");
    String exp = Db.getSql("kcgl-list-exp");
    if (StrKit.notBlank(keyWord)) {
      exp +=
          "\n" + "AND (kc.title like concat('%','" + keyWord
              + "','%') or  kc.content like concat('%','" + keyWord + "','%'))";
    }
    if (StrKit.notBlank(openid)) {
      sec += ",ku.status type,t.time";
      table +=
          "\n LEFT JOIN  kcgl_user ku ON kc.id = ku.kid" + " AND ku.openid='"
              + openid + "'";
      table +=
          "\n LEFT JOIN kcgl_time t ON kc.id = t.kid" + " AND t.openid='"
              + openid + "'";
      if (StrKit.notBlank(status)) {
        exp +=
            "\n" + " AND ku.openid='" + openid + "'"
                + " AND ku.status='" + status + "'";
      }
    }
    exp += " order by kc.create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, table + exp);
    return Models.compressPage(pages);
  }

  public static ModelResult xzList(String openid) {
    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = null;
    String sec = Db.getSql("xz-list-sec");
    List<Kcgl> list = dao.find(sec, openid, "success");
//    Log.d("xzList: " + list);
    if (null != list && list.size() > 0) {
      for (int i = 0; i < list.size(); i++) {
        Kcgl kcgl = list.get(i);
        int kid = kcgl.getId();
        boolean r = isAll(kid, openid);
        kcgl.put("isStartAll", r);
      }
    }
    result.setObject(list);
    result.setResult(Result.kSuccess);

    return result;
  }

  public static boolean isAll(int kid, String openid) {
    boolean r = false;
    List<Steps> list_steps = Steps.getAllListByKid(kid, openid);
    List<Steps> list_steps_my = Steps.getMyListByKid(kid, openid, "end");
//    Log.d("list_steps: " + list_steps);
//    Log.d("list_steps_my: " + list_steps_my);
    if (null != list_steps && null != list_steps_my
        && list_steps.size() == list_steps_my.size()) {
      r = true;
    }
    return r;
  }

  public static ModelResult query(int id, String openid) {

    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = null;
    String sql = Db.getSql("kcgl-query");
    List<Kcgl> list = dao.find(sql, id);
    if (null != list && list.size() > 0) {
      object = Models.compressList(list);
//      object.put("steps", Steps.getList(id,openid));
      List<Steps> list_steps = Steps.getList(id, openid);
      List<Steps> list_steps_user = Steps.getListByOpenid(id, openid);
      for (int i = 0; i < list_steps.size(); i++) {
        Steps steps = list_steps.get(i);
        int sid = steps.getId();
        for (int k = 0; k < list_steps_user.size(); k++) {
          Steps step_user = list_steps_user.get(k);
          steps.put("type", "");
          int suid = step_user.getId();
          String type = step_user.get("type", "");
          if (sid == suid) {
            steps.put("type", type);
            break;
          }
        }
      }
//      Log.d("list_steps: " + list_steps);
      object.put("steps", list_steps);

      if (StrKit.notBlank(openid)) {
        object.put("type", KcglUser.getStatusByOpenid(id, openid));
        object.put("time", KcglTime.getTimeByOpenid(id, openid));
      }
    }
    if (null != object) {
      result.setObject(object);
      result.setResult(Result.kSuccess);
    }
    return result;
  }
}
